<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    showFooter
    :title="getTitle"
    width="30%"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm">
      <template #requestPathSlot="{ model, field }">
        <a-input v-model:value="model[field]">
          <template #addonBefore>
            <a-select
              v-model:value="serviceName"
              :options="servicesOptions"
              @change="handleChange"
              style="width: 120px"
            />
          </template>
        </a-input>
        <div>{{ requestPath }}</div>
      </template>
      <template #permsSlot="{ model, field }">
        <a-input v-model:value="model[field]" @change="handleChangeInput">
          <template #addonAfter>
            <a-select
              v-model:value="permsType"
              :options="permsTypeOptions"
              @change="handleChange"
              style="width: 150px"
            />
          </template>
        </a-input>
        <div>{{ permsText }}</div>
      </template>
    </BasicForm>
  </BasicDrawer>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { Select, Input } from 'ant-design-vue';
  import { BasicForm, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';

  import { permsFormSchema, permsTypeOptions, servicesOptions } from './menu.data';
  import { getAllMenuList } from '/@/api/sys/menu';
  import { saveOrUpdateMenu } from './menu.api';

  export default defineComponent({
    name: 'MenuPermsDrawer',
    components: {
      BasicDrawer,
      BasicForm,
      [Select.name]: Select,
      [Input.name]: Input,
      [Input.Group.name]: Input.Group,
    },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);
      const serviceName = ref('');
      const requestPath = ref('');
      const permsText = ref('');
      const perms = ref('');
      const permsType = ref('');

      const [
        registerForm,
        { resetFields, setFieldsValue, getFieldsValue, updateSchema, validate },
      ] = useForm({
        labelWidth: 100,
        schemas: permsFormSchema,
        showActionButtonGroup: false,
      });

      const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
        resetFields();
        setDrawerProps({ confirmLoading: false });
        isUpdate.value = !!data?.isUpdate;

        if (unref(isUpdate)) {
          setFieldsValue({
            ...data.record,
          });
        } else {
          setFieldsValue({
            parentId: data.record.parentId,
          });
        }
        const treeData = await getAllMenuList();
        updateSchema({
          field: 'parentId',
          componentProps: { treeData },
        });
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '新增菜单' : '编辑菜单'));

      async function handleSubmit() {
        try {
          const values = await validate();
          setDrawerProps({ confirmLoading: true });
          await saveOrUpdateMenu(values, isUpdate.value);
          closeDrawer();
          emit('success');
        } finally {
          setDrawerProps({ confirmLoading: false });
        }
      }

      function handleChange(val) {
        const values = getFieldsValue();
        if (values.permsPrefix) {
          permsText.value = values.permsPrefix;
          if (val) {
            permsText.value = values.permsPrefix + ':' + val;
          }
        }
        setFieldsValue({ perms: permsText.value });
      }

      function handleChangeInput() {
        const values = getFieldsValue();
        if (values.permsPrefix) {
          permsText.value = values.permsPrefix + ':' + permsType.value;
        }
        setFieldsValue({ perms: permsText.value });
      }

      return {
        registerDrawer,
        registerForm,
        handleSubmit,
        handleChange,
        handleChangeInput,
        getTitle,
        perms,
        permsText,
        permsType,
        permsTypeOptions,
        servicesOptions,
        requestPath,
        serviceName,
      };
    },
  });
</script>
